home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / misc / emu / multicv101.lha / multicv.c next >
C/C++ Source or Header  |  1995-06-07  |  11KB  |  378 lines

  1. /*
  2.  * MultiCV v1.01 - Copyright (C) 1995 by Giuseppe Ghibo'
  3.  *
  4.  * Converts ASCII files from/to the systems Amiga, Macintosh and DOS.
  5.  *
  6.  * Written by Giuseppe Ghibo' <ghibo@galileo.polito.it>
  7.  *
  8.  * Version 1.01 - 7 Jun 1995
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #ifndef min
  17. #define min(a,b) (a < b ? a : b)
  18. #endif
  19.  
  20. enum {DOS2AMI, AMI2DOS, DOS2MAC, MAC2DOS, AMI2MAC, MAC2AMI, NOPT};
  21.  
  22. struct convopt {
  23.     int val;
  24.     char *opt;
  25.     char *desc;
  26. };
  27.  
  28. struct convopt convopt[NOPT] = {
  29.     { DOS2AMI, "dos2ami", "Convert DOS file to AMIGA file" },
  30.     { AMI2DOS, "ami2dos", "Convert AMIGA file to DOS file" },
  31.     { DOS2MAC, "dos2mac", "Convert DOS file to MACINTOSH file" },
  32.     { MAC2DOS, "mac2dos", "Convert MACINTOSH file to DOS file" },
  33.     { AMI2MAC, "ami2mac", "Convert AMIGA file to MACINTOSH file" },
  34.     { MAC2AMI, "mac2ami", "Convert MACINTOSH file to AMIGA file" }
  35. };
  36.  
  37. struct mybuf
  38. {
  39.     int n;
  40.     unsigned char *buf;
  41. };
  42.  
  43. static unsigned char charset[NOPT][256]= {
  44. { /* dos2ami */
  45.    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  46.    25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
  47.    46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
  48.    67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  49.    88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
  50.    107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
  51.    123,124,125,126,127,199,252,233,226,228,224,229,231,234,235,232,
  52.    239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,
  53.    162,163,165, 80,102,225,237,243,250,241,209,170,186,191, 45,172,
  54.    189,188,161,171,187, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  55.     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  56.     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  57.     32, 32, 32, 32, 32, 97,223, 71,182, 83,115,181,116,248, 81, 32,
  58.    240, 32,248, 69, 32, 61,177, 62, 60, 32, 32,247, 61,176,183,183,
  59.     86,110,178, 32,160 },
  60. { /* ami2dos */
  61.    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  62.    25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
  63.    46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
  64.    67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  65.    88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
  66.    107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
  67.    123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,
  68.    139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,
  69.    155,156,157,158,159,32 ,173,155,156,164,157,166,167,168,169,166,
  70.    174,170,173,174,175,248,241,253,179,180,230,227,249,184,185,167,
  71.    175,172,171,190,168,192,193,194,195,142,143,146,128,200,144,202,
  72.    203,204,205,206,207,208,165,210,211,212,213,153,215,216,217,218,
  73.    219,154,221,222,225,133,160,131,227,132,134,145,135,138,130,136,
  74.    137,141,161,140,139,240,164,149,162,147,245,148,246,248,151,163,
  75.    150,129,253,254,152},
  76. { /* dos2mac */
  77.    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  78.    25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
  79.    46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
  80.    67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  81.    88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
  82.    107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
  83.    123,124,125,126,127,130,159,142,137,138,136,140,141,144,145,143,
  84.    149,148,147,128,129,131,207,206,153,154,152,158,157,216,133,134,
  85.    162,163,180, 80,196,135,146,151,156,150,132,187,188,192, 45,194,
  86.     32, 32,193,199,200, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  87.     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  88.     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  89.     32, 32, 32, 32, 32, 97,167, 71,185,183,115,181,116, 70, 81,189,
  90.    182,176,197, 69, 32,217,177,179,178, 32, 32,214,197,161,165,165,
  91.    195,110, 50,165,202},
  92. { /* mac2dos */
  93.    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  94.    25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
  95.    46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
  96.    67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  97.    88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
  98.    107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
  99.    123,124,125,126,127,142,143,128,144,165,153,154,160,133,131,132,
  100.    131,134,135,130,138,136,137,161,141,140,139,164,162,149,147,148,
  101.    147,163,151,150,129, 43,248,155,156, 21,249, 20,225, 82, 67, 32,
  102.     39, 34, 61,146,232,236,241,243,242,157,230,235,228,185,185, 83,
  103.    166,167,234,145,237,168,173,194,251,159,237, 30,174,175, 95,255,
  104.     65, 65, 79, 32, 32, 45,196, 34, 34, 96, 39,246, 32,152,240,179,
  105.     32, 60, 62, 32, 32, 32,250, 44, 32, 37, 65, 69, 65, 69, 69, 73,
  106.     73, 73, 73, 79, 79, 32, 79, 85, 85, 85, 49, 94, 94, 45, 32, 32,
  107.    248, 32, 34, 32, 32},
  108. { /* ami2mac */
  109.    0,1,2,3,4,5,6,7,8,9,13,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  110.    25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
  111.    46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
  112.    67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  113.    88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
  114.    107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
  115.    123,124,125,126,127, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  116.     32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  117.     32, 32, 32, 32, 32,202,193,162,163,215,180,124,164,172,169,187,
  118.    199,194, 45,168,208,161,177, 50, 51,171,181,166,165, 44, 49,188,
  119.    200, 32, 32, 32,192,203,203,204,204,128,129,174,130,131,131, 69,
  120.     69, 73, 73, 73, 73, 68,132,152,151,153,155,133,120,175,157,156,
  121.    158,159, 89, 80,167,136,135,137,139,138,140,190,141,143,142,144,
  122.    145,147,146,148,149,182,150,152,151,153,155,154,214,191,157,156,
  123.    158,159,121,112,216},
  124. { /* mac2ami */
  125.    0,1,2,3,4,5,6,7,8,9,10,11,12,10,14,15,16,17,18,19,20,21,22,23,24,
  126.    25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
  127.    46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
  128.    67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  129.    88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,
  130.    107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
  131.    123,124,125,126,127,196,197,199,201,209,214,220,225,224,226,228,
  132.    227,229,231,233,232,234,235,237,236,238,239,241,243,242,244,246,
  133.    245,250,249,251,252, 43,176,162,163,167,183,182,223,174,169,174,
  134.    180,168, 61,198,216, 32,177, 60, 62,165,181,240, 83,182,182, 83,
  135.    170,186, 32,230,248,191,161,172, 86,102,248, 32,171,187, 95,160,
  136.    192,195,213,198,230, 45, 45, 34, 34, 96, 39,247, 32,255, 61,166,
  137.    164, 60, 62, 32, 32, 32,183,184, 32, 37,194,202,193,203,200,205,
  138.    206,207,204,211,212, 32,210,218,219,217,185, 94, 94,175, 32, 32,
  139.    176, 32, 32, 32, 32}
  140. };
  141.  
  142. #define LF '\012'
  143. #define CR '\015'
  144.  
  145. #ifdef MSDOS
  146. #define DIMBUF 16000
  147. #define OPTCHAR '/'
  148. #define READBIN "rb"
  149. #define WRITEBIN "wb"
  150. #else
  151. #define DIMBUF 60000
  152. #define OPTCHAR '-'
  153. #define READBIN "r"
  154. #define WRITEBIN "w"
  155. #endif
  156.  
  157. #define DOUBDIMBUF    2*DIMBUF
  158. #define FNAMEIN        argv[2]
  159. #define FNAMEOUT    argv[3]
  160.  
  161. char tmpbuf[1024];
  162.  
  163. void usage_exit(void);
  164. struct mybuf *convert(struct mybuf *inbuf, int opt);
  165.  
  166.  
  167. void main(int argc, char **argv)
  168. {
  169.     FILE *fhin, *fhout;
  170.     struct mybuf inbuf, *outbuf;
  171.     int i, opt;
  172.  
  173.     fprintf(stderr,"MultiCV v1.01 - (C) 1995 by Giuseppe Ghibo'\n\n");
  174.  
  175.     if (argc == 1)
  176.         usage_exit();
  177.  
  178.     if (argc > 4)
  179.     {
  180.         fprintf (stderr,"Bad arguments!\n\n");
  181.         usage_exit();
  182.     }
  183.  
  184.     for (i = 0; i < NOPT; i++)
  185.     {
  186.         if (stricmp(&argv[1][1],convopt[i].opt) == NULL)
  187.         {
  188.             opt = i;
  189.             break;
  190.         }
  191.     }
  192.  
  193.     if (i >= NOPT)
  194.     {
  195.         fprintf(stderr,"Bad arguments: `%s'\n\n", argv[1]);
  196.         usage_exit();
  197.     }
  198.  
  199.     if (argc == 2 || argc == 3)
  200.     {
  201.         fhin = stdin;
  202.         fhout = stdout;
  203.     }
  204.  
  205.     if (argc == 4)
  206.     {
  207.         if ((fhin = fopen(FNAMEIN, READBIN)) == NULL)
  208.         {
  209.             sprintf(tmpbuf, "multicv: error: Can't open input file '%s'", FNAMEIN);
  210.             perror(tmpbuf);
  211.             exit(1);
  212.         }
  213.  
  214.         if ((fhout = fopen(FNAMEOUT, WRITEBIN)) == NULL)
  215.         {
  216.             sprintf(tmpbuf, "multicv: error: Can't open output file '%s'", FNAMEOUT);
  217.             perror(tmpbuf);
  218.             exit(1);
  219.         }
  220.     }
  221.  
  222.     if ((inbuf.buf = malloc(DIMBUF)) == NULL)
  223.     {
  224.         fprintf(stderr, "multicv: error: Can't alloc %u bytes of memory\n", DIMBUF);
  225.         exit(1);
  226.     }
  227.     
  228.     while ((inbuf.n = fread(inbuf.buf, 1, DIMBUF, fhin)) > 0)
  229.     {
  230.  
  231.         outbuf = convert(&inbuf, opt);
  232.  
  233.         fwrite(outbuf->buf, 1, outbuf->n, fhout);
  234.  
  235.         if (outbuf != &inbuf)
  236.         {
  237.             free(outbuf -> buf);
  238.             free(outbuf);
  239.         }
  240.     }
  241.  
  242.     if (inbuf.buf)
  243.         free(inbuf.buf);
  244.  
  245.     if (fhin != stdin)
  246.         fclose(fhin);
  247.  
  248.     if (fhout != stdout)
  249.         fclose(fhout);
  250.  
  251. }
  252.  
  253. void usage_exit(void)
  254. {
  255.     int i;
  256.  
  257.     fprintf(stderr, "Usage:\tmulticv %c<opt> <filein> <fileout>\n", OPTCHAR);
  258.     fprintf(stderr, "Where <opt> is one of:\n");
  259.  
  260.     for (i = 0; i < NOPT; i++)
  261.         fprintf(stderr,"\t%s - %s\n",convopt[i].opt, convopt[i].desc);
  262.         
  263.     exit(0);
  264. }
  265.  
  266. struct mybuf *convert(struct mybuf *inbuf, int opt)
  267. {
  268.     struct mybuf *outbuf;
  269.     register unsigned char *p, *q, *r;
  270.  
  271.     if ((opt == AMI2DOS) || (opt == MAC2DOS))
  272.     {
  273.         if ((outbuf = malloc(sizeof(struct mybuf))) == NULL)
  274.         {
  275.             fprintf(stderr, "multicv: error: Can't alloc %u bytes of memory!\n", sizeof(struct mybuf));
  276.             exit(1);
  277.         }
  278.  
  279.         if ((outbuf->buf = malloc(DOUBDIMBUF)) == NULL)
  280.         {
  281.             fprintf(stderr, "multicv: error: Can't alloc %u bytes of memory!\n", DOUBDIMBUF);
  282.             exit(1);
  283.         }
  284.     }  
  285.     else
  286.         outbuf = inbuf;
  287.  
  288.     switch(opt)
  289.     {
  290.         case AMI2MAC: case MAC2AMI:
  291.  
  292.             q = inbuf->buf + inbuf->n;
  293.  
  294.             for (p = inbuf->buf; p < q; p++)
  295.                 *p = charset[opt][*p];
  296.  
  297.             break;
  298.  
  299.         case DOS2AMI:
  300.  
  301.             q = inbuf->buf + inbuf->n;
  302.  
  303.             for (p = r = inbuf->buf; p < q; p++, r++)
  304.             {
  305.                 if (*p == CR)
  306.                 {
  307.                     *r = LF;
  308.                     p++;    /* skip next LF */
  309.                 }
  310.                 else
  311.                     *r = charset[opt][*p];
  312.             }
  313.             outbuf->n = r - outbuf->buf;
  314.  
  315.             break;
  316.  
  317.         case DOS2MAC:
  318.  
  319.             q = inbuf->buf + inbuf->n;
  320.  
  321.             for (p = r = inbuf->buf; p < q; p++, r++)
  322.             {
  323.                 if (*p == CR)
  324.                 {
  325.                     *r = CR;
  326.                     p++;    /* skip next LF */
  327.                 }
  328.                 else
  329.                     *r = charset[opt][*p];
  330.             }
  331.             outbuf->n = r - outbuf->buf;
  332.  
  333.             break;
  334.  
  335.         case AMI2DOS:
  336.  
  337.             q = inbuf->buf + inbuf->n;
  338.  
  339.             for (p = inbuf->buf, r = outbuf->buf; p < q; p++, r++)
  340.             {
  341.                 if (*p == LF)
  342.                     *r++ = CR;
  343.  
  344.                 *r = charset[opt][*p];
  345.             }
  346.  
  347.             outbuf->n = r - outbuf->buf;
  348.  
  349.             break;
  350.  
  351.         case MAC2DOS:
  352.  
  353.             q = inbuf->buf + inbuf->n;
  354.  
  355.             for (p = inbuf->buf, r = outbuf->buf; p < q; p++, r++)
  356.             {
  357.                 if (*p == CR)
  358.                 {
  359.                     *r++ = CR;
  360.                     *r = LF;
  361.                 }
  362.                 else
  363.                     *r = charset[opt][*p];
  364.             }
  365.  
  366.             outbuf->n = r - outbuf->buf;
  367.  
  368.             break;
  369.  
  370.         default:
  371.  
  372.             break;
  373.  
  374.     }
  375.     
  376.     return(outbuf);
  377. }
  378.